#pragma once

#ifndef __PROJECT_ITEM_PROPS__INCLUDED__HH__
#define __PROJECT_ITEM_PROPS__INCLUDED__HH__
 
#include <Ogre.h>
#include <QtGui>
#include "SettingsTable.h"
#include "ZoomWidget.h"
#include "OgreSubWidget.h"
#include "ProjectItem.h"
#include <LitEntity.h>
#include <LitLight.h>
#include "RenderViewport.h"

class Project;
class ProjectItemPreview : public OgreSubWidget, public Ogre::FrameListener
{
	Q_OBJECT
	Lit::EntityPtr			mEntity;
	Lit::LightPtr			mLight;
	Lit::RefPtr<Ogre::SceneNode>	mSceneNode;
	MouseDrag						mDrag;
	void preViewportUpdate(const Ogre::RenderTargetViewportEvent& evt);
    void postViewportUpdate(const Ogre::RenderTargetViewportEvent& evt);
public:
	ProjectItemPreview( QWidget*parent );
	~ProjectItemPreview();
	
	const Lit::EntityPtr& getEntity() const;
	Lit::EntityPtr& getEntity();
	
	void mousePressEvent( QMouseEvent*evt )
	{
		mDrag.mousePressed( evt->pos(), evt->buttons() );
	}
	void mouseMoveEvent( QMouseEvent*evt )
	{
		mDrag.mouseMoved( evt->pos(), evt->buttons() );
	}
	void mouseReleaseEvent( QMouseEvent*evt )
	{
		mDrag.mouseReleased( evt->pos(), evt->buttons() );
	}
	const Lit::LightPtr& getLight() const;
	Lit::LightPtr& getLight();

	bool frameRenderingQueued( const Ogre::FrameEvent& evt );
	
	void replaceMesh( const std::string &mesh );
	void fitEntity() ;

protected slots:
	void ogreInitialise();
	void ogreShutdown();
};

typedef Lit::RefPtr<ProjectItemPreview> ProjectItemPreviewPtr;

class ProjectItemProperties : public SettingsWidget
{
	Q_OBJECT

protected:
	ProjectNodePtr			mCurrentNode;
	ProjectItemPreview *	mPreview;
	RenderViewport*			mViewport;
	Project*				mProject;

protected slots:
	void propertyChanged( const std::string &page, const std::string &name, const std::string &value );
	void pageUpdateRequested( int );
	void subEntitySettingChanged(const std::string&, const std::string&);

public:
	ProjectItemProperties( QWidget*parent );
	~ProjectItemProperties();
	
	RenderViewport* viewport();
	void setViewport( RenderViewport * vp );
	
	void setProject( Project* proj );

	ProjectItemPreview* preview();
	void setPreview( ProjectItemPreview * prev );
	void updateProperties( ProjectNodePtr& item );
	void updateSelectionProperties( Lit::SceneSelectionPtr& item );
	SettingsWidget::Page * updatePageProperties( SettingsWidget::Page * page, Lit::MovablePtr node );
};

typedef Lit::RefPtr<ProjectItemProperties> ProjectItemPropertiesPtr;

#endif